package com.zlyx.easy.jedis.supports;

import org.springframework.context.ApplicationContext;

import com.zlyx.easy.asyn.supports.AbstractMsgListener;
import com.zlyx.easy.core.refresh.IHandlerOnRefreshed;
import com.zlyx.easy.core.utils.ThreadUtils;
import com.zlyx.easy.jedis.utils.JedisUtils;

import redis.clients.jedis.JedisPubSub;

/**
 * @Auth 赵光
 * @Describle
 * @2019年9月7日
 */
public class JedisMsgListener extends AbstractMsgListener implements IHandlerOnRefreshed {

	private String jedisChannelKey;

	public JedisMsgListener(String jedisChannelKey) {
		this.jedisChannelKey = jedisChannelKey;
	}

	@Override
	public void doOnRefreshed(ApplicationContext context) throws Exception {
		ThreadUtils.execute(new Thread(() -> {
			JedisUtils.subscribe(new JedisListener(), jedisChannelKey);
		}));
	}

	public class JedisListener extends JedisPubSub {

		@Override
		public void onMessage(String channel, String message) {
			handleMsg(message);
		}
	}
}
